<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="assetgroupList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" :label="$t('名称')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.name" :content="scope.row.name" class="wid190" :refName="scope.row.name">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="desc" :label="$t('描述')" align='center' min-width="200">
        <template slot-scope="scope">
          <tooltip :html="scope.row.desc" :content="scope.row.desc" class="wid190" :refName="scope.row.desc">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="member" :label="$t('资产对象名称')" align='center' min-width="200">
        <template slot-scope="scope">
          <tooltip :html="classMapShow(scope.row,';<br/>')" :content="classMapShow(scope.row,',')" class="wid190" :refName="scope.row.name"></tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ref" :label="$t('引用计数')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.ref" :content="scope.row.ref" class="wid190" :refName="scope.row.ref">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" :class="'edit' + scope.$index" @click="dialogVisible(scope.row)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination> -->

    <Assetgroup-dialog v-if="assetgroupDialogVisible" @listData='listData' ref="refAssetgroupDialogVisible"></Assetgroup-dialog>
  </div>
</template>
<script>
import { getAssetGroupArray, delAssetGroup } from '@/api/list/assets/assetGroup'
import AssetgroupDialog from './components/AssetgroupDialog'
export default {
  name: 'Assetgroup',
  components: {
    AssetgroupDialog
  },
  data () {
    return {
      assetgroupDialogVisible: false,
      assetgroupList: [],
      // total: 0,
      selectList: [],
      name: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // handleSizeChange (val) {
    //   // console.log(`每页 ${val} 条`)
    //   this.record.limit = val
    //   this.listData()
    // },
    // handleCurrentChange (val) {
    //   // console.log(`当前页: ${val}`)
    //   this.record.page = val
    //   this.listData()
    // },
    // 多选按钮
    handleSelectionChange (data) {
      this.selectList = data
      var name = ''
      for (var item of data) {
        if (name === '') {
          name = item.id
        } else {
          name = name + '@#@' + item.id
        }
      }
      this.name = name
    },
    // 列表
    listData () {
      this.assetgroupDialogVisible = false
      getAssetGroupArray().then(res => {
        this.assetgroupList = res.info
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.assetgroupDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refAssetgroupDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.name) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delAssetGroup({ id: this.name }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
            }
            this.name = ''
            this.listData()
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    classMapShow (data, str) {
      let reStr = ''
      for (const index in data.member) {
        if (index === String(data.member.length - 1)) {
          if (str === ',') {
            reStr += data.member[index]
          } else {
            reStr += data.member[index] + str
          }
        } else {
          reStr += data.member[index] + str
        }
      }
      return reStr
    }
  }
}
</script>
